package jsptest;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet(name = "MyServlet", urlPatterns = "/MyServlet")
public class MyServlet extends HttpServlet {

    // 处理GET请求
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 设置响应内容类型（可选）
        // response.setContentType("text/html;charset=utf-8");

        // 向请求作用域设置属性（存储数据）
        request.setAttribute("username", "itcast");
        request.setAttribute("password", "202212390606");

        // 获取请求转发器，指向目标JSP页面
        RequestDispatcher dispatcher = request.getRequestDispatcher("/myjsp-1.jsp");

        // 执行请求转发（包含请求和响应对象）
        dispatcher.forward(request, response);
    }

    // 处理POST请求（直接调用doGet方法）
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}